<template>
  <!--还价-->
  <el-dialog
    :close-on-click-modal="false"
    title="还价"
    :visible.sync="dialogVisible3"
    width="400px"
  >
    <div>
      <el-form
        size="mini"
        :model="sendForm3"
        label-width="100px"
        ref="sendForm3"
        :rules="rules3"
      >
        <el-form-item label="品名牌号："
          >{{ sendForm3.goodsName }}{{ sendForm3.goodsSn }}</el-form-item
        >
        <el-form-item label="数量（吨）：">{{
          sendForm3.goodsNum
        }}</el-form-item>
        <el-form-item label="报价：">{{
          sendForm3.purchasePrice
        }}</el-form-item>
        <el-form-item label="出价：" prop="reducePrice">
          <el-input
            type="number"
            @change="checkInput"
            v-model="sendForm3.reducePrice"
            style="width: 150px"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose3">取 消</el-button>
      <el-button type="primary" @click="handleConfirm3" :loading="btnLoading3"
        >确认还价</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { inquiryOperate } from "@/api/allInLine/inquiry";
import { mapGetters } from "vuex";
import { Message } from "element-ui";

const defaultSendForm3 = {
  inquiryId: "",
  operateType: "inquiryReduce",
  reducePrice: "",
  purchasePrice: "",
  unOrderReason: "",
  goodsName: "",
  goodsSn: "",
  goodsNum: "",
  priceId: "",
};

export default {
  name: "index",

  data() {
    const isvalidateInput = (rule, value, callback) => {
      if (value > this.sendForm3.purchasePrice) {
        callback(new Error("还价金额不能高于报价"));
      } else {
        callback();
      }
    };

    return {
      dialogVisible3: false,
      btnLoading3: false,
      sendForm3: JSON.parse(JSON.stringify(defaultSendForm3)),
      rules3: {
        reducePrice: [
          { required: true, message: "请填写还价金额", trigger: "blur" },
          { validator: isvalidateInput, trigger: "blur" },
        ],
      },

      purchasePrice: "",
      inquiryId: "",
      type: "inquiryReduce",
    };
  },
  computed: {
    ...mapGetters(["name", "mobile", "userId"]),
  },
  components: {},
  created() {},
  filters: {},
  methods: {
    checkInput() {
      if (this.sendForm3.reducePrice < 0) {
        this.sendForm3.reducePrice = "0";
      }
      if (this.sendForm3.reducePrice > 999999) {
        this.sendForm3.reducePrice = "999999";
      }
    },

    //显示还价
    showPop3(item) {
      this.dialogVisible3 = true;
      this.sendForm3.goodsName = item.goodsName;
      this.sendForm3.goodsSn = item.goodsSn;
      this.sendForm3.goodsNum = item.goodsNum;
      this.sendForm3.purchasePrice = item.purchasePrice;
      this.sendForm3.inquiryId = item.inquiryId;
      this.sendForm3.priceId = item.priceId;
    },
    //关闭还价
    handleClose3() {
      this.dialogVisible3 = false;
      this.$refs.sendForm3.resetFields;
      this.sendForm3 = JSON.parse(JSON.stringify(defaultSendForm3));
    },

    //确认还价
    handleConfirm3() {
      this.$refs.sendForm3.validate((valid) => {
        if (valid) {
          this.btnLoading3 = true;
          const sendForm3 = JSON.parse(JSON.stringify(this.sendForm3));
          const data = {
            inquiryId: sendForm3.inquiryId,
            operateType: sendForm3.operateType,
            reducePrice: sendForm3.reducePrice,
            unOrderReason: "",
            purchasePrice: sendForm3.purchasePrice,
            priceId: sendForm3.priceId,
          };
          inquiryOperate(data)
            .then((res) => {
              Message.success("提交成功");
              this.$refs.sendForm3.resetFields;
              this.sendForm3 = JSON.parse(JSON.stringify(defaultSendForm3));
              this.dialogVisible3 = false;
              this.$emit("update");
              this.btnLoading3 = false;
            })
            .catch((err) => {
              this.btnLoading3 = false;
            });
        }
      });
    },
  },
};
</script>

<style  lang="scss">
</style>
